﻿using System;
using UnityEngine;

namespace AI.Graph {


    [System.Serializable]
    public class ActiveTimer {

        [NonSerialized]
        private bool m_isActive;

        [NonSerialized]
        private float m_timeLeft;

        [NonSerialized]
        private float m_lastUpdateTime;

        /// <summary>
        /// Updates the active time.
        /// Returns true if this timer is active.
        /// </summary>
        public bool isActive {
            get {
                if (!Application.isPlaying) {
                    return false;
                }

                m_timeLeft -= Time.time - m_lastUpdateTime;
                m_lastUpdateTime = Time.time;

                if (m_timeLeft < 0) {
                    m_timeLeft = 0;
                }

                m_isActive = m_timeLeft > 0;

                return m_isActive;
            }
        }


        // Resets the active time of the given amount.
        public void SetActive (float activeTime = 0.2F) {
            m_timeLeft = activeTime;
            m_isActive = true;
        }

    }


}